package com.vg.spinner;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.content.SharedPreferences;
import android.widget.Spinner;

import com.androidhive.jsonparsing.JSONParser;
import com.androidhive.jsonparsing.MyAdapter;
import com.androidhive.jsonparsing.R;
import com.vg.config.linkUrl;

public class spinner_drop extends Activity {
	
	private static final String PHRASE = "Item_";
	private String prefBody = "PrefBody";
	private String prefCity = "PrefCity";
	private String prefColor = "PrefColor";
	private String prefDrinker = "PrefDrinker";
	private String prefEducation = "PrefEducation";
	private String prefGender = "PrefGender";
	private String prefMaritalStatus = "PrefMaritalStatus";
	private String prefHeight = "PrefHeight";
	private String prefWeight = "PrefWeight";
	private String prefReligion = "PrefReligion";
	private String prefSmoker = "PrefSmoker";
	private String prefUsertype = "PrefUsertype";
	
	MyAdapter adapter;
	JSONParser jparse;
	JSONArray jsa;
	linkUrl mUrl;
	private ArrayList<Student> array;

	public void putPref(String pref, String url) {
		jparse = new JSONParser();
		jsa = jparse.getJsonarray(url);
		SharedPreferences prefs;
		prefs = getSharedPreferences(pref, MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		for (int i = 0; i < jsa.length(); i++) {
			try {
				editor.putString(PHRASE + i + "", jsa.getString(i));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		editor.commit();
	}
	
	public void dropItems(Spinner sp, String pref, ArrayList<Student> arr) {
		array = new ArrayList<Student>();
		adapter = new MyAdapter(this, R.layout.drop_gender, arr, true);
		sp.setAdapter(adapter);
		SharedPreferences prefs;
		prefs = getSharedPreferences(pref, MODE_PRIVATE);
		for(int i = 0; i < prefs.getAll().size(); i++) {    
            Student n = new Student(prefs.getString(PHRASE + i, ""));
            array.add(n);
        }
		adapter.notifyDataSetChanged();
	}
	
	public ArrayList<Student> dropSpinner(String pref) {
		ArrayList<Student> arrayList=new ArrayList<Student>();
		SharedPreferences prefs;
		prefs = getSharedPreferences(pref, MODE_PRIVATE);
		for(int i = 0; i < prefs.getAll().size(); i++) {    
            Student n = new Student(prefs.getString(PHRASE + i + "", ""));
            arrayList.add(n);
        }
		return arrayList;
	}
}
